using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace SymAutomate.JobFile
{
    /// <summary>
    /// Error Numbers 320-349; Reference file ErrorNumbers.txt for more information.
    /// 320 Error: JobFileParams format Invalid!
    /// </summary>
    class JobFileParameter
    {
        public class JobFileParameterSegment
        {
            private string __Prompt;
            private object __Value;

            public string Prompt { get { return __Prompt; } set { __Prompt = value; } }
            public object Value { get { return __Value; } set { __Value = value; } }

            public JobFileParameterSegment(string _Prompt, object _Value)
            {
                __Prompt = _Prompt;
                __Value = _Value;
            }
        }

        private List<JobFileParameterSegment> __JobFileParameterSegmentCollection;
        
        public List<JobFileParameterSegment> JobFileParameterSegmentCollection { get { return __JobFileParameterSegmentCollection; } }

        public JobFileParameter()
        {
            __JobFileParameterSegmentCollection = new List<JobFileParameterSegment>();
        }

        public JobFileParameter(string _RawCommand)
        {
            __JobFileParameterSegmentCollection = new List<JobFileParameterSegment>();
            this.ParseRawCommand(_RawCommand);
        }

        public void ParseRawCommand(string _RawCommand)
        {
            if (!string.IsNullOrEmpty(_RawCommand))
            {
                MatchCollection _MatchCollection;
                Regex _Regex;

                //TODO: Validate Regex Format

                //get the first command
                _Regex = new Regex(@"^{(.*?)}{(.*?)}");
                _MatchCollection = _Regex.Matches(_RawCommand);
                if (_MatchCollection.Count == 1)
                {
                    __JobFileParameterSegmentCollection.Add(new JobFileParameterSegment(_MatchCollection[0].Groups[1].ToString(), _MatchCollection[0].Groups[2].ToString()));
                }
                else
                {
                    throw new ReturnException("321 Error: JobFileParams format Invalid!");
                }

                _Regex = new Regex(@",{(.*?)}{(.*?)}");
                _MatchCollection = _Regex.Matches(_RawCommand);
                for (int i = 0; i < _MatchCollection.Count; i++)
                {
                    __JobFileParameterSegmentCollection.Add(new JobFileParameterSegment(_MatchCollection[i].Groups[1].ToString(), _MatchCollection[i].Groups[2].ToString()));
                }

                //foreach (JobFileParameterSegment _J in __JobFileParameterSegmentCollection)
                //{
                //    Console.WriteLine(_J.Prompt + "-" + _J.Value); 
                //}
            }
            else
            {
                throw new ReturnException("320 Error: JobFileParams format Invalid!");
            }
        }

       
    }
}
